package com.datawin.taotaocheche.ui.fragment.presenter;

import com.datawin.taotaocheche.base.BaseBiz;
import com.datawin.taotaocheche.bean.UserInfo;
import com.datawin.taotaocheche.comm.MyApplication;
import com.datawin.taotaocheche.ui.actviity.login.CheckAct;
import com.datawin.taotaocheche.ui.actviity.user.AboutAct;
import com.datawin.taotaocheche.ui.actviity.user.BillAct;
import com.datawin.taotaocheche.ui.actviity.user.CertificationAct;
import com.datawin.taotaocheche.ui.actviity.user.CollectionAct;
import com.datawin.taotaocheche.ui.actviity.user.MsgAct;
import com.datawin.taotaocheche.ui.actviity.user.RecharegAct;
import com.datawin.taotaocheche.ui.actviity.user.SettingAct;
import com.datawin.taotaocheche.ui.actviity.user.SuggestionAct;
import com.datawin.taotaocheche.ui.actviity.user.WebViewAct;
import com.datawin.taotaocheche.ui.fragment.biz.MineBiz;
import com.datawin.taotaocheche.ui.fragment.contract.MineContract;
import com.datawin.taotaocheche.util.FileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by chenjian on 17/11/30.
 */

public class MinePresent implements MineContract.Presenter {
    MineContract.View view;
    MineBiz mineBiz;

    @Inject
    public MinePresent(MineContract.View view) {
        this.view = view;
    }

    @Override
    public void setBiz(BaseBiz biz) {
        mineBiz = (MineBiz) biz;
    }

    @Override
    public void updateImage(String code, List<String> photoInfos) {
        view.loading(true);
        Map<String, RequestBody> params = new HashMap<>();
        params.put("userCode", FileUtils.convertToRequestBody(code));
        List<File> fileList = new ArrayList<>();
        for (int i = 0; i < photoInfos.size(); i++) {
            File file = new File(photoInfos.get(i));//filePath 图片地址
            fileList.add(file);
        }
        List<MultipartBody.Part> partList = FileUtils.filesToMultipartBodyParts(fileList);
        mineBiz.updateImg(params, partList, new BaseBiz.Callback<Object>() {
            @Override
            public void onSuccess(Object o) {
                view.loading(false);
                getUserInfo();
            }

            @Override
            public void onFailure(Object o) {
                view.loading(false);
            }
        });
    }


    @Override
    public void getUserInfo() {
        mineBiz.getUserInfo(MyApplication.userInfo.getUserCode(), new BaseBiz.Callback<List<UserInfo>>() {
            @Override
            public void onSuccess(List<UserInfo> o) {
                view.upDateUI(o.get(0));
            }

            @Override
            public void onFailure(List<UserInfo> o) {

            }
        });
    }

    @Override
    public void toCollection() {
        view.startAct(CollectionAct.class, null);
    }

    @Override
    public void toBill() {
        view.startAct(BillAct.class, null);
    }

    @Override
    public void toMsg() {
        view.startAct(MsgAct.class, null);
    }

    @Override
    public void toSuggestion() {
        view.startAct(SuggestionAct.class, null);
    }

    @Override
    public void toAbout() {
        view.startAct(AboutAct.class, null);
    }

    @Override
    public void toRecharge() {
        view.startAct(RecharegAct.class, null);
    }

    @Override
    public void toReal() {
        view.startAct(CheckAct.class, null);
    }

    @Override
    public void toSetting() {
        view.startAct(SettingAct.class, null);
    }

    @Override
    public void toInvitation() {
        view.startAct(WebViewAct.class, null);
    }
}
